#include "Thread.h"

#include <process.h>

Thread::Thread()
: m_bRun(true), m_hThread(NULL)
{
}

Thread::~Thread()
{
	if( m_hThread != NULL )
	{
		CloseHandle(m_hThread);
		m_hThread = NULL;
	}
}

bool Thread::Run()
{
	typedef unsigned (__stdcall *THREADFUNC)(void*);
	m_hThread = (HANDLE)_beginthreadex(NULL, 0, (THREADFUNC)&Thread::_WorkThread, this, 0, &m_uiId);
	if( m_hThread == NULL )
		return false;

	return true;
}

void Thread::Stop()
{
	m_bRun = false;
}


uint Thread::_WorkThread(void *pvArg)
{
	Thread *t = (Thread *)pvArg;

	while(true)
	{
		if( t->m_bRun == false )
			break;

		t->Worker();
	}
	return 0;
}
